package com.itany.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Author：汤小洋
 * Date：2022-11-22 16:09
 * Description：IoC容器工具类，用于获取并操作IoC容器
 */
@Component
public class ApplicationContextHolder implements ApplicationContextAware {

    private static ApplicationContext ac;

    /**
     * 会自动注入当前的IoC容器
     */
    @Override
    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        ApplicationContextHolder.ac = ac;
    }

    /**
     * 获取IoC容器中的bean
     */
    public static Object getBean(String beanName){
        return ac.getBean(beanName);
    }

    public static <T> T getBean(Class<T> tClass){
        return ac.getBean(tClass);
    }

}
